package com.example.myshools.Activities;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.myshools.R;

import java.util.HashMap;
import java.util.Map;

public class ActivitiesFragment extends Fragment {
    private class MyTabSpec{
        private TextView textView=null;
        private int normal;
        private int select;
        private Fragment fragment=null;

        public TextView getTextView() {
            return textView;
        }

        public void setTextView(TextView textView) {
            this.textView = textView;
        }

        public int getNormal() {
            return normal;
        }

        public void setNormal(int normal) {
            this.normal = normal;
        }

        public int getSelect() {
            return select;
        }

        public void setSelect(boolean b) {
            if(b){
                textView.setTextColor(Color.parseColor("#00ff00"));
            }else {
                textView.setTextColor(Color.parseColor("#000000"));
            }
        }

        public Fragment getFragment() {
            return fragment;
        }

        public void setFragment(Fragment fragment) {
            this.fragment = fragment;
        }
    }
    private Map<String,MyTabSpec> map=new HashMap<>();
    private String[] tabStrId={"最新","校级","院级","社团","第二课堂","综测"};
    private Fragment curFragment=null;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View newView = inflater.inflate(R.layout.activity_fragment, container, false);




        return newView;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        changeTab(tabStrId[0]);


    }

    private void changeTab(String s) {
        changeFragment(s);
        changeColor(s);
    }

    private void changeColor(String s) {
        for (String key : map.keySet()) {
            map.get(key).setSelect(false);
        }
        map.get(s).setSelect(true);
    }

    private void changeFragment(String s) {
        Fragment fragment=map.get(s).getFragment();
        if (curFragment==fragment) {return;}
        //FragmentTransaction transaction= getChildFragmentManager().beginTransaction();
        FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
       /* List<Fragment> fragments=getChildFragmentManager().getFragments();
        for(Fragment fragment1:fragments){
            transaction.remove(fragment1);
        }*/
        if(curFragment!=null){
            transaction.remove(curFragment);
        }
        if(!fragment.isAdded()){
            transaction.add(R.id.tab_head_content,fragment);
        }
        transaction.show(fragment);
        curFragment=fragment;
        transaction.commit();


    }

    private class MyListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.news_now:
                    changeTab(tabStrId[0]);
                    break;
                case R.id.school_1:
                    changeTab(tabStrId[1]);
                    break;
                case R.id.school_2:
                    changeTab(tabStrId[2]);
                    break;
                case R.id.school_3:
                    changeTab(tabStrId[3]);
                    break;
                case R.id.school_4:
                    changeTab(tabStrId[4]);
                    break;
                case R.id.school_5:
                    changeTab(tabStrId[5]);
                    break;
            }
        }
    }
    private void initData() {
        map.put(tabStrId[0], new MyTabSpec());
        map.put(tabStrId[1], new MyTabSpec());
        map.put(tabStrId[2], new MyTabSpec());
        map.put(tabStrId[3], new MyTabSpec());
        map.put(tabStrId[4], new MyTabSpec());
        map.put(tabStrId[5], new MyTabSpec());

        setFragment();

        findView();
        
    }

    private void setFragment() {
        map.get(tabStrId[0]).setFragment(new NewFragment());
        map.get(tabStrId[1]).setFragment(new SchoolFragment());
        map.get(tabStrId[2]).setFragment(new CollegeFragment());
        map.get(tabStrId[3]).setFragment(new ClubFragment());
        map.get(tabStrId[4]).setFragment(new OutScoreFragment());
        map.get(tabStrId[5]).setFragment(new CompFragment());
    }
    private void findView(){

        TextView tv1 = getView().findViewById(R.id.news_now);
        TextView tv2 = getView().findViewById(R.id.school_1);
        TextView tv3 = getView().findViewById(R.id.school_2);
        TextView tv4 = getView().findViewById(R.id.school_3);
        TextView tv5 = getView().findViewById(R.id.school_4);
        TextView tv6 = getView().findViewById(R.id.school_5);

        map.get(tabStrId[0]).setTextView(tv1);
        map.get(tabStrId[1]).setTextView(tv2);
        map.get(tabStrId[2]).setTextView(tv3);
        map.get(tabStrId[3]).setTextView(tv4);
        map.get(tabStrId[4]).setTextView(tv5);
        map.get(tabStrId[5]).setTextView(tv6);

        MyListener myListener=new MyListener();
        tv1.setOnClickListener(myListener);
        tv2.setOnClickListener(myListener);
        tv3.setOnClickListener(myListener);
        tv4.setOnClickListener(myListener);
        tv5.setOnClickListener(myListener);
        tv6.setOnClickListener(myListener);



    }



}
